package www.yingdi365.com.viewholder;

import android.content.Intent;
import android.net.Uri;
import android.os.Process;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.lzy.imagepicker.ui.ImageGridActivity;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import www.yingdi365.com.R;
import www.yingdi365.com.activity.DengLuActivity;
import www.yingdi365.com.activity.SheZhiActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.customview.TwoBtnDialog;
import www.yingdi365.com.model.AppUser;
import www.yingdi365.com.util.ACache;

/**
 * Created by Administrator on 2017/3/28 0028.
 */
public class SheZhiViewHolder extends BaseViewHolder<AppUser> implements View.OnClickListener {

    private final ImageView imagePhoto;
    private final TextView textName;
    private final TextView textBirthday;
    private final TextView textSex;

    public SheZhiViewHolder(ViewGroup parent, @LayoutRes int res) {
        super(parent, res);
        imagePhoto = $(R.id.imagePhoto);
        textName = $(R.id.textName);
        textBirthday = $(R.id.textBirthday);
        textSex = $(R.id.textSex);
        $(R.id.viewHaoPing).setOnClickListener(this);
        $(R.id.buttonTuiChu).setOnClickListener(this);
        $(R.id.viewTouXiang).setOnClickListener(this);
    }

    @Override
    public void setData(AppUser data) {
        super.setData(data);
        Glide.with(getContext())
                .load(data.getPhoto())
                .bitmapTransform(new CropCircleTransformation(getContext()))
                .placeholder(R.mipmap.ic_empty)
                .into(imagePhoto);
        String name = data.getName();
        if (TextUtils.isEmpty(name)) {
            name = "暂无昵称";
        }
        String sex = data.getSex();
        if (TextUtils.isEmpty(sex)) {
            sex = "暂无";
        }
        textName.setText(name);
        textSex.setText(sex);
        String birthday = data.getBirthday();
        textBirthday.setText(birthday.substring(0, 10));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.viewTouXiang:
                Intent intent = new Intent();
                intent.setClass(getContext(), ImageGridActivity.class);
                ((SheZhiActivity)getContext()).startActivityForResult(intent, Constant.REQUEST_RESULT_CODE.IMAGE_PICKER);
                break;
            case R.id.viewHaoPing:
                String mAddress = "market://details?id=" + getContext().getPackageName();
                Intent marketIntent = new Intent("android.intent.action.VIEW");
                marketIntent.setData(Uri.parse(mAddress));
                getContext().startActivity(marketIntent);
                break;
            case R.id.buttonTuiChu:
                final TwoBtnDialog twoBtnDialog = new TwoBtnDialog(getContext(), "提示", "（确认退出该账号吗？）", "确认", "取消");
                twoBtnDialog.setClicklistener(new TwoBtnDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        ACache aCache = ACache.get(getContext(), Constant.ACACHE.YING_DI);
                        aCache.clear();
                        Intent intent = new Intent(getContext(), DengLuActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        getContext().startActivity(intent);
                        // 杀掉进程
                        Process.killProcess(Process.myPid());
                        System.exit(0);
                    }

                    @Override
                    public void doCancel() {
                        twoBtnDialog.dismiss();
                    }
                });
                twoBtnDialog.show();
                break;

        }
    }
}
